package org.dromara.timemate.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.ValidatorUtils;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.core.validate.QueryGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.timemate.domain.bo.DataAcquisitionBo;
import org.dromara.timemate.domain.bo.DeviceManageBo;
import org.dromara.timemate.domain.vo.DataAcquisitionVo;
import org.dromara.timemate.domain.vo.DeviceManageVo;
import org.dromara.timemate.domain.vo.DeviceVo;
import org.dromara.timemate.service.DataAcquisitionService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/tm/dataAcquisition")
public class DataAcquisitionController extends BaseController {
    @Resource
    private DataAcquisitionService dataAcquisitionService;

    /**
     * 通过手机号（模糊查询）、设备名称、设备类型、采集时间、进行分页查询
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo<DataAcquisitionVo> list(@Validated(QueryGroup.class) DataAcquisitionBo bo, PageQuery pageQuery) {
        return dataAcquisitionService.queryPageList(bo, pageQuery);
    }

    /**
     * 获得所有设备信息
     * @return
     */
    @GetMapping("/allDeviceInfo")
    public Collection<DeviceManageVo> queryAllDeviceInfo()
    {
        Collection<DeviceManageVo> deviceVos = dataAcquisitionService.queryAllDeviceInfo();
        return deviceVos;
    }

    /**
     * 根据设备id，和采集时间进行添加对应的指标
     * @param dataAcquisitionBo
     * @return
     */
    @PostMapping("/add")
    public R<Void> addDeviceManageInfo(@Valid @RequestBody DataAcquisitionBo dataAcquisitionBo) {
        ValidatorUtils.validate(dataAcquisitionBo, AddGroup.class);
        dataAcquisitionService.addDataAcquisition(dataAcquisitionBo);
        return R.ok("操作成功");
    }

    /**
     * 根据设备id和采集时间进行修改对应的指标
     * @param dataAcquisitionBo
     * @return
     */
    @RepeatSubmit
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DataAcquisitionBo dataAcquisitionBo) {
        dataAcquisitionService.updateByBo(dataAcquisitionBo);
        return R.ok("操作成功");
    }

    /**
     * 根据设备id和采集时间进行删除对应的指标
     * @param list
     * @return
     */
    @DeleteMapping("/batchDelete")
    public R<Void> remove(@Validated(AddGroup.class)@RequestBody List<DataAcquisitionBo> list) {
        dataAcquisitionService.deleteWithValidByIds(list);
        return  R.ok("操作成功");
    }

}
